#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>

int main(int argc, char const *argv[])
{
    struct hostent *h;

    if(argc != 2) {
        fprintf(stderr, "usage: ./dns_test1 address\n");
        exit(1);
    }

    if((h = gethostbyname(argv[1])) == NULL) {
        herror("gethostbyname");
        exit(1);
    }

    printf("Host name:%s\n", h->h_name);
    // printf("IP Address:%s\n", inet_ntoa(*((struct in_addr*)h->h_addr)) );

    struct in_addr* ptr = h->h_addr; // 此次转换不损失精度，因为h->h_addr指针指向的数组占32位，包含所有部分的ip

    in_addr_t addr = *(h->h_addr_list[0]); // 此次转换损失精度，因为*(h->h_addr_list[0])只占8位，只包含第一部分的ip
    // in_addr_t占32位 h_>h_addr_list[0]为"www.a.shifen.com"，而*(h->h_addr_list[0])只占一个字母，代表一个char
    struct in_addr temp;
    temp.s_addr = addr;

    int size = sizeof(char); // char只占 1byte=8bit
    int size = sizeof(in_addr_t); // in_addr_t = uint32_t = 32bit

// (base) hjn@ubuntu:~/Desktop/c/chapter6/chapter6_7$ ./dns_test1 www.baidu.com
// Host name:www.a.shifen.com
// IP Address:14.0.0.0
// IP Address:14.215.177.38


    printf("IP Address:%s\n", inet_ntoa(temp) );
    printf("IP Address:%s\n", inet_ntoa(*ptr) );
// #ifdef __USE_MISC
// # define	h_addr	h_addr_list[0] /* Address, for backward compatibility.*/
// #endif

    return 0;
}
